using System.Collections.Generic;
using UnityEngine;

/// <summary> 图标类型枚举 </summary>
public enum IconType{Player = 0, Pet = 1}


[System.Serializable]
public partial class IconData
{
    [SerializeField] private int _ID; // IconID
    [SerializeField] private string _ResName; // Icon资源名
    [SerializeField] private IconType _Type; // 图标类型
    [SerializeField] private Vector2 _Pos; // 图片位置
    [SerializeField] private Vector3 _Wpos; // 图片世界位置
    [SerializeField] private PropSet _PropSet; // 属性信息

    #region 获取和应用属性区域
    /// <summary> IconID </summary>
    public int ID { get { return _ID; }}

    /// <summary> Icon资源名 </summary>
    public string ResName { get { return _ResName; }}

    /// <summary> 图标类型 </summary>
    public IconType Type { get { return _Type; }}

    /// <summary> 图片位置 </summary>
    public Vector2 Pos { get { return _Pos; }}

    /// <summary> 图片世界位置 </summary>
    public Vector3 Wpos { get { return _Wpos; }}

    /// <summary> 属性信息 </summary>
    public PropSet PropSet { get { return _PropSet; }}


    /// <summary> 生成一个Data </summary>
    public static IconData CreateData(
            int lID, // IconID
            string lResName, // Icon资源名
            IconType lType, // 图标类型
            Vector2 lPos, // 图片位置
            Vector3 lWpos, // 图片世界位置
            PropSet lPropSet // 属性信息
        )
    {
        IconData lIconData = new IconData();
        lIconData._ID = lID;
        lIconData._ResName = lResName;
        lIconData._Type = lType;
        lIconData._Pos = lPos;
        lIconData._Wpos = lWpos;
        lIconData._PropSet = lPropSet;
        return lIconData;
    }
    #endregion
}

[System.Serializable]
public partial class IconDataManager : ScriptableObject
{
    /// <summary> 索引是否初始化 </summary>
    public bool initializedDataMap = false;


    /// <summary> 数据信息 </summary>
    [SerializeField] private List<IconData> _dataList = new List<IconData>();


    /// <summary> 数据索引 </summary>
    private Dictionary<int, int> _dataMap;


    /// <summary> 数据索引 </summary>
    private Dictionary<int, int> DataMap
    {
        get
        {
            if (!initializedDataMap)
            {
                _dataMap = new Dictionary<int, int>();

                for (int i = 0, iMax = _dataList.Count; i < iMax; ++i)
                {
                    # if UNITY_EDITOR
                    if (_dataMap.ContainsKey(_dataList[i].ID))
                    {
                        Debug.LogError("[Error]IconDataManager::Init(). 配置ID有重复! id = " + _dataList[i].ID);
                    }
                    #endif

                    _dataMap[_dataList[i].ID] = i;
                }

                initializedDataMap = true;
            }

            return _dataMap;
        }
    }


    /// <summary> 获取所有数据 </summary>
    public List<IconData> GetDataAll()
    {
        return _dataList;
    }


    /// <summary> 是否包含该ID数据 </summary>
    public bool Contains(int id)
    {
        return DataMap.ContainsKey(id);
    }


    /// <summary> 通过ID获取数据 </summary>
    public IconData GetData(int id)
    {
        int index;
        if (null == DataMap || !DataMap.TryGetValue(id, out index))
        {
            Debug.LogError("[Error]IconDataManager::GetData(). data is not exist! ID = " + id);
            return null;
        }

        return _dataList[index];
    }
}
